(function() {
    angular.module('app').component('brandControllerComponent', {
        templateUrl: 'app/controllers/brand.controller/brand.controller.component/brand.controller.component.html',
        controller: brandControllerComponent,
        controllerAs: 'vm',
        bindings: {
            getType: '<', //componentType
            monitorBrandAcount: '='
        }
    });

    brandControllerComponent.$inject = ['brandControllerComponentService', '$scope', '$loading', '$rootScope', '$timeout', 'PagerService', 'util', '$controller'];

    function brandControllerComponent(brandControllerComponentService, $scope, $loading, $rootScope, $timeout, PagerService, util, $controller) {

        var vm = this;

        vm.$onInit = function() {
            if (vm.getType === "brand") {

                angular.extend(vm, $controller('commonCtrl', { vm: vm }));

                console.log(vm);
                //设置分页函数实现
                vm.pager.pagerChange = pagerChange;


                pagerChange();

            } else {
                //monitor.controller
                vm.deleteCollection = deleteCollection;
                monitorControllerInit();
            }
        };

        //brand data json
        vm.table = {
            count: 0,
            brands: []
        };


        //var BrandList = $resource(url, paramDefaults, actions)


        //分页
        function pagerChange() {

            $loading.start('brandsList');

            console.log();

            //angular.extend(vm.param,vm.pager);
            vm.param = util.extend(vm.param, vm.pager);

            console.log('------------pagerChange---------');

            console.log(vm.param);
            //vm.param ajax请求的参数
            brandControllerComponentService.get(vm.param).then(function(data) {

                // console.log(data);

                var data = data.value;

                vm.table.brands.length = 0;

                vm.table.count = data.count;

                vm.pager.setAllPageCount(data.count);

                vm.table.brands = data.brands;

                angular.forEach(vm.table.brands, function(item, index) {
                    if (item.sellAmount == 0) {

                        item.avgPrice = 0;

                    } else {

                        item.avgPrice = (item.sellMoney / item.sellAmount).toFixed(2);

                    }
                });

                brandControllerComponentService.brandBasicInit(); //初始化

                $timeout(function() {
                    $loading.finish('brandsList');
                }, 500);

            }, function(err) {
                vm.table.brands.length = 0;
                $loading.finish('brandsList');
            });

        }

        //排序
        vm.sort = sort; //function



        //品牌
        vm.getBrandBasic = brandControllerComponentService.getBrandBasic; //function

        //品牌数据
        vm.getBrandDetail = _getBrandDetail; //function



        function sort(sortColumn) {

            if (vm.filter.sortColumn == sortColumn) {
                return;
            }
            vm.filter.sortColumn = sortColumn;
            init();
        }




        function _getBrandDetail(brandName) {
            $rootScope.$emit('formBrand_detail_EV', brandName);
        }

        /*-------------------------------monitor.controller--------------------------------*/



        function monitorControllerInit() {

            brandControllerComponentService.getCollection().then(function(data) {

                if ($.isEmptyObject(data)) {
                    vm.table.brands = [];

                    vm.monitorBrandAcount = 0;

                    return;
                }

                vm.table.brands = data.collBrands;

                //bind
                vm.monitorBrandAcount = data.collBrands.length;

                angular.forEach(vm.table.brands, function(item, index) {
                    if (item.sellAmount == 0) {

                        item.avgPrice = 0;

                    } else {

                        item.avgPrice = (item.sellMoney / item.sellAmount).toFixed(2);

                    }
                });

                brandControllerComponentService.brandBasicInit(); //初始化

            }, function(err) {});
        }

        function deleteCollection(brand) {
            brandControllerComponentService.delCollection(brand).then(function() {

                var index = vm.table.brands.indexOf(brand);

                vm.table.brands.splice(index, 1);
            });
        }
    }
})();
